﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Bus.Controllers;
using Way.Bus.WMS.Model;
using Way.EAP.DataAccess.Data;
using System.Data;
using Way.EAP.DataAccess.Entity;
using Way.Contract.Data;
using Way.Contract.MVC;
using Way.Contract.Data.ControllerData;
using Way.Bus.Model;

namespace Way.Bus.WMS.Controllers
{
    public class DifferenceController : BusinessController
    {
        /// <summary>
        /// 盘点差异控制器
        /// 创建时间：2013-06-07
        /// </summary>
        public DifferenceController() : base(new Difference()) { }
        public DifferenceController(IModel model) : base(model) { }

        public override bool IsReviewedState
        {
            get
            {
                return false;
            }
        }

        public override bool IsClearAway
        {
            get
            {
                return false;
            }
        }

        protected override string OnControllerName()
        {
            return "盘点差异管理";
        }

        //菜单中url路径
        protected override string OnGetPath()
        {
            return "Views/WMS/Check/Difference.htm";
        }
        //开发人
        protected override string OnGetAuthor()
        {
            return "";
        }
        //说明
        protected override string OnGetControllerDescription()
        {
            return "盘点差异管理";
        }

        #region 初始化数据方法
        protected override void OnInitViewChildItem(ModelData data, ItemData item)
        {
            base.OnInitViewChildItem(data, item);
            if (data.name.EndsWith("Difference"))
            {
                switch (item.field.ToLower())
                {
                    case "isdisable":
                        item.visible = false;
                        break;
                    case "sourcecode":
                        item.visible = true;
                        item.title = "盘点单号";
                        break;
                    case "code":
                        item.visible = true;
                        item.title = "盘点差异单号";
                        break;
                }

            }
            if (data.name.EndsWith("DifferenceMaterials"))
            {
                item.disabled = true;
                switch (item.field.ToLower())
                {
                    case "submiteddate":
                    case "submitedby":
                    case "createdby":
                    case "creationdate":
                    case "modifiedby":
                    case "modifieddate":
                    case "reviewedby":
                    case "revieweddate":
                    case "isdisable":
                    case "isdelete":
                    case "issubmited":
                    case "isreviewed":
                    case "sourcecode":
                    case "code":
                    case "stay1":
                    case "stay2":
                    case "stay3":
                    case "stay4":
                    case "stay5":
                    case "portcode":
                    case "parentid":
                        item.visible = false;
                        break;

                    case "materialcode":
                        item.index = 1;
                        break;
                    case "newmcode":
                        item.index = 2;
                        break;
                    case "newfmodel":
                        item.index = 3;
                        break;
                    case "pdepotwbs":
                        item.index = 4;
                        break;
                    case "pbatchno":
                        item.index = 5;
                        break;
                    case "pnum":
                        item.index = 6;
                        break;
                    case "kmaterialcode":
                        item.index = 7;
                        break;
                    case "kmcode":
                        item.index = 8;
                        break;
                    case "kpdepotwbs":
                        item.index = 9;
                        break;
                    case "kbatchno":
                        item.index = 10;
                        break;
                    case "knum":
                        item.index = 11;
                        break;
                    case "cnum":
                        item.index = 12;
                        item.disabled = false;
                        break;
                    case "updatenum":
                        item.index = 13;
                        break;
                    case "remark":
                        item.disabled = false;
                        break;
                }

            }

        }

        #endregion

        [ActionCommand(name = "导出", title = "导出", index = 10, icon = "icon-down", isselectrow = false, onclick = "emport6")]
        public void Emport()
        {
            //ExcelDRController er = new ExcelDRController();
            //er.bc = this;
        }

        protected override void OnAdding(ControllerBase.SaveEvent item)
        {
            ValidateItem(item);
            base.OnAdding(item);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="item"></param>
        protected override void OnEditing(SaveEvent item)
        {
            ValidateItem(item);
            base.OnEditing(item);
        }

        #region
        /// <summary>
        /// 验证数据正确性
        /// </summary>
        /// <param name="item"></param>
        private void ValidateItem(ControllerBase.SaveEvent item)
        {
            Difference bc = item.Item as Difference;
            foreach (var i in bc.Materials)
            {

                if (i.UPDATENUM < 0)
                {
                    throw new Exception("异常：变更数量不能小于0");
                }
                if (i.UPDATENUM > i.PNUM)
                {
                    throw new Exception("异常：变更数量大于盘点数量");
                }
            }

        }

        private void CreateInorder(BasicInfo info)
        {
            Difference df = info as Difference;
            EntityList<DifferenceMaterials> dfmList = new EntityList<DifferenceMaterials>(this.model.GetDataAction());
            //创建入库单
            dfmList.GetData(" CNUM>0 and PARENTID=" + df.ID);
            StockInOrder sio = new StockInOrder();
            sio.Createdby = user.ID;
            sio.Creationdate = DateTime.Now;
            sio.Remark = "库存调整创建入库单";

            BillNumberController bnc = new BillNumberController();
            sio.Code = bnc.GetBillNo(this);
            for (int i = 0; i < dfmList.Count; i++)
            {
                StockInOrderMaterials siom = new StockInOrderMaterials();
                siom.DEPOTWBS = "1";
                siom.MATERIALCODE = dfmList[i].MATERIALCODE;
                siom.NUM = dfmList[i].CNUM;
                sio.Materials.Add(siom);
            }
            EntityList<StockInOrder> ninorder = new EntityList<StockInOrder>(this.model.GetDataAction());
            ninorder.Add(sio);
            ninorder.Save();
            string strsql = "insert into Acc_WMS_StockInOrder_Materials(DEPOTWBS,MATERIALCODE,NUM,PARENTID) ";
            strsql += "(select '1',Code,NUM*(-1),'" + sio.ID + "' from Acc_WMS_StockInfo_Materials where NUM<0 and ID not in (select KID from Acc_WMS_DifferenceMaterials))";
            this.model.GetDataAction().Execute(strsql);
        }

        private void CreateOutorder(BasicInfo info)
        {
            Difference df = info as Difference;
            EntityList<DifferenceMaterials> dfmList = new EntityList<DifferenceMaterials>(this.model.GetDataAction());
            //创建出库单
            dfmList.GetData(" CNUM<0 and PARENTID=" + df.ID);
            StockOutOrder sio = new StockOutOrder();
            sio.Createdby = user.ID;
            sio.Creationdate = DateTime.Now;
            sio.Remark = "库存调整创建出库单";

            BillNumberController bnc = new BillNumberController();
            sio.Code = bnc.GetBillNo(this);
            for (int i = 0; i < dfmList.Count; i++)
            {
                StockOutOrderMaterials siom = new StockOutOrderMaterials();
                siom.DEPOTWBS = "1";
                siom.MATERIALCODE = dfmList[i].MATERIALCODE;
                siom.NUM = dfmList[i].CNUM * (-1);
                sio.Materials.Add(siom);
            }
            EntityList<StockOutOrder> ninorder = new EntityList<StockOutOrder>(this.model.GetDataAction());
            ninorder.Add(sio);
            ninorder.Save();
            string strsql = "insert into Acc_WMS_StockOutOrderMaterials(DEPOTWBS,MATERIALCODE,NUM,PARENTID) ";
            strsql += "(select '1',Code,NUM,'" + sio.ID + "' from Acc_WMS_StockInfo_Materials where NUM>0 and ID not in (select KID from Acc_WMS_DifferenceMaterials))";
            this.model.GetDataAction().Execute(strsql);
        }

        #endregion
    }
}
